/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.component.log.maintain;

import java.util.Iterator;
import java.util.List;

import com.jinhe.tss.component.log.LogConstants;
import com.jinhe.tss.component.log.entity.Log;
import com.jinhe.tss.component.support.web.webwork.PTActionSupport;
import com.jinhe.tss.core.web.dispaly.grid.GridDataEncoder;
import com.jinhe.tss.core.web.dispaly.xform.IXForm;
import com.jinhe.tss.core.web.dispaly.xform.XFormEncoder;

public class LogAction extends PTActionSupport {

    private static final long serialVersionUID = 1151920949189953182L;

    private Long  id;
    private int   page = 1;
    
    private LogQueryCondition condition = new LogQueryCondition();
    
    private LogService service;

    public String getAllApps4Tree(){
        List<?> data = service.getAllApps();
        
        StringBuffer sb = new StringBuffer("<actionSet><treeNode name=\"全部\" id=\"_rootId\">");
        for(Iterator<?> it = data.iterator(); it.hasNext();){
            String appCode = (String) it.next();
            sb.append("<treeNode id=\"" + appCode + "\" name=\"" + appCode + "\" icon=\"images/app.gif\"/>");
        }
        return print("AppTree", sb.append("</treeNode></actionSet>"));
    }
    
    public String queryLogs4Grid(){
        condition.setPagesize(LogConstants.PAGE_SIZE);
        condition.setCurrentPage(page);
        Object[] objs = service.getLogsByCondition(condition);
        
        GridDataEncoder encoder = new GridDataEncoder(objs[0], LogConstants.LOG_GRID_TEMPLET_PATH);
        
        int totalRows = (Integer) objs[1];
        int currentPageRows = ((List<?>) objs[0]).size();
        
        String pageInfo = generatePageInfo(totalRows, page, LogConstants.PAGE_SIZE, currentPageRows);
        return print(new String[]{"LogList", "PageList"}, new Object[]{encoder.toXml(), pageInfo});
    }
    
    public String getLogInfo(){
        Log log = service.getLogById(id);          
        return print("LogInfo", new XFormEncoder(LogConstants.LOG_XFORM_TEMPLET_PATH, (IXForm) log));
    }
 
    public void setService(LogService service) {
        this.service = service;
    }
 
    public LogQueryCondition getCondition() {
        return condition;
    }
 
    public void setPage(int currentPageNum) {
        this.page = currentPageNum;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
}

